package com.rf.activiti.control;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.rf.activiti.service.ActModelService;
import com.rf.sys.frm.BaseController;
import com.rf.sys.utils.JsonUtils;
import com.rf.sys.utils.PageUtils;


/**
 * 流程模型相关Controller
 * @author dehuisun
 * @version 2016-11-03
 */
@Controller
@RequestMapping(value = "/act/model")
public class ActModelController extends BaseController {

	@Autowired
	private ActModelService actModelService;

	/**
	 * 流程模型列表
	 */
    @RequestMapping(value = "query")
	@ResponseBody
	public void modelList(PageUtils pageUtils,  HttpServletRequest request, HttpServletResponse response) {
    	org.activiti.engine.repository.Model model;
    	Map searchMap = actModelService.modelList(null,pageUtils);
//		model.addAttribute("page", page);
//		model.addAttribute("category", category);
//		Map searchMap = actModelService.getDemoPage(Demo, pageUtils);
    	super.responsePrint(request, response, JsonUtils.mapToJson(searchMap));
//		return "modules/act/actModelList";
	}

//	/**
//	 * 创建模型
//	 */
//	@RequestMapping(value = "create", method = RequestMethod.GET)
//	public String create(Model model) {
//		return "modules/act/actModelCreate";
//	}
	
	/**
	 * 创建模型
	 */
	@RequestMapping(value = "create", method = RequestMethod.POST)
	public void create(String id,String name, String key, String description, String category,
			HttpServletRequest request, HttpServletResponse response) {
			JSONObject returnObj = new JSONObject();
		try {
			if(id!=null&&!id.equals("")){
				actModelService.updateCategory(id, category);
			}else{
				actModelService.create(name, key, description, category);
			}
//			response.sendRedirect(request.getContextPath() + "/act/process-editor/modeler.jsp?modelId=" + modelData.getId());
			returnObj.element("httpCode", "OK");//成功
		} catch (Exception e) {
			returnObj.element("httpCode", "ERROR");//成功
			e.printStackTrace();
		}finally{
			super.responsePrint(request, response, returnObj.toString());
		}
		
	}

	/**
	 * 根据Model部署流程
	 */
	@RequestMapping(value = "deploy")
	public String deploy(String id, RedirectAttributes redirectAttributes) {
		String message = actModelService.deploy(id);
		redirectAttributes.addFlashAttribute("message", message);
		return "redirect:/act/process";
	}
	
	/**
	 * 导出model的xml文件
	 */
	@RequestMapping(value = "export")
	public void export(String id, HttpServletResponse response) {
		actModelService.export(id, response);
	}

	/**
	 * 更新Model分类
	 */
	@RequestMapping(value = "updateCategory")
	public void updateCategory(String id, String category, HttpServletRequest request, HttpServletResponse response) {
		JSONObject returnObj = new JSONObject();
		try{
			actModelService.updateCategory(id, category);
		returnObj.element("httpCode", "OK");//成功
		} catch (Exception e) {
		returnObj.element("httpCode", "ERROR");//成功
		e.printStackTrace();
	}finally{
		super.responsePrint(request, response, returnObj.toString());
	}
	}
	
	/**
	 * 删除Model
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "delete")
	public void delete(String ids, HttpServletRequest request, HttpServletResponse response) {
		JSONObject returnObj = new JSONObject();
		try{
		actModelService.delete(ids);
		returnObj.element("httpCode", "OK");//成功
		} catch (Exception e) {
		returnObj.element("httpCode", "ERROR");//成功
		e.printStackTrace();
	}finally{
		super.responsePrint(request, response, returnObj.toString());
	}
	}
	/**
	 * 获取流程
	 * @param id
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "getModel")
	public void getModel(String id, HttpServletRequest request,HttpServletResponse response) throws Exception {
		JSONObject returnObj = new JSONObject();
		try {
			org.activiti.engine.repository.Model modleDate = actModelService.getModel(id);
			JSONObject data = JSONObject.fromObject(modleDate);
			returnObj.element("data", data);// 成功
			returnObj.element("httpCode", "OK");// 成功
		} catch (Exception e) {
			returnObj.element("httpCode", "ERROR");// 成功
			e.printStackTrace();
		} finally {
			super.responsePrint(request, response, returnObj.toString());
		}
	}
}
